local DIY = require "packages.diy_utility.diy_utility"
local RUtil = require "packages.aaa_Romantic.utility.rmt_util"

local liuhe = fk.CreateSkill{
  name = "rmt__liuhe",
  tags = { Skill.Limited },
}

Fk:loadTranslationTable{
  ["rmt__liuhe"] = "六合",
  [":rmt__liuhe"] = "昂扬技，每回合结束时，你可以获得中央区花色不同的牌各一张。激昂：使用六张牌。",

  ["@rmt__liuhe"] = "六合",

  ["rmt__liuhe_poxi"] = "六合选牌",
  ["#rmt__liuhe_poxi"] = "六合：可以获得中央区花色不同的牌各一张",

  ["$rmt__liuhe1"] = "",
  ["$rmt__liuhe2"] = "",
}

Fk:addPoxiMethod{
  name = "rmt__liuhe_poxi",
  prompt = "#rmt__liuhe_poxi",
  card_filter = function (to_select, selected, data, extra_data)
    if data == nil then return false end
    if #selected == 0 then return true end
    for _, dat in ipairs(data) do
      local ids = dat[2]
      if table.contains(ids, to_select) then
        return not table.find(selected, function (id)
          return table.contains(ids, id)
        end)
      end
    end
    return true
  end,
  feasible = function (selected)
    if #selected == 0 then return false end
    local map = {}
    for _, id in ipairs(selected) do
      local card = Fk:getCardById(id)
      map[card:getSuitString()] = (map[card:getSuitString()] or 0) + 1
      if map[card:getSuitString()] == 2 then
        return false
      end
    end
    return true
  end,
}

liuhe:addLoseEffect(function (self, player)
  player.room:setPlayerMark(player, "@rmt__liuhe", 0)
end)

liuhe:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

liuhe:addEffect(fk.TurnEnd, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player)
    return player:hasSkill(liuhe.name) and player:usedSkillTimes(liuhe.name, Player.HistoryGame) == 0
    and #DIY.getCenterCards(player.room) > 0
  end,
  on_cost = function (self, event, target, player)
    local room = player.room
    local cardsMap = {}
    for _, id in ipairs(DIY.getCenterCards(room)) do
      local suit = Fk:getCardById(id):getSuitString(true)
      cardsMap[suit] = cardsMap[suit] or {}
      table.insert(cardsMap[suit], id)
    end

    local card_data = {}
    for suit, cards in pairs(cardsMap) do
      table.insert(card_data, {suit, cards})
    end

    if #card_data == 0 then return end
    local cards = room:askToPoxi(player, {
      poxi_type = "rmt__liuhe_poxi",
      data = card_data,
      cancelable = true,
    })
    if #cards > 0 then
      event:setCostData(self, cards)
      return true
    end
  end,
  on_use = function (self, event, target, player)
    local room = player.room
    room:setPlayerMark(player, "@rmt__liuhe", 6)
    room:obtainCard(player, event:getCostData(self), true, fk.ReasonJustMove, player, liuhe.name)
  end
})

liuhe:addEffect(fk.CardUseFinished, {
  can_refresh = function (self, event, target, player)
    return player:getMark("@rmt__liuhe") > 0 and player == target
  end,
  on_refresh = function (self, event, target, player)
    player.room:removePlayerMark(player, "@rmt__liuhe", 1)
    if player:getMark("@rmt__liuhe") == 0 then
      RUtil.resetSkill(player, liuhe.name)
    end
  end
})

return liuhe